import school
import grade
import classroom
import teacher 
import student

import xml.dom.minidom


"""
parse the input xml file and load each objects into schools
"""
def loadxml(schools, infile):
    dom = xml.dom.minidom.parse(infile)
    
    for school_node in dom.getElementsByTagName("school"):
        school_id = school_node.getAttribute("id")
        schools[school_id] = school.school(school_id, school_node.getAttribute("name"))
        
        #print(school_node.childNodes)
        for grade_node in school_node.childNodes:
            if(grade_node.nodeType == grade_node.ELEMENT_NODE):
                grade_obj = grade.grade(grade_node.getAttribute("id"))
                schools[school_id].add_grade(grade_obj)
                
                for classroom_node in grade_node.getElementsByTagName("classroom"):
                    classroom_id = classroom_node.getAttribute("id")
                    classroom_obj = classroom.classroom(classroom_id,classroom_node.getAttribute("name"))
                    grade_obj.add_classroom(classroom_obj) 
                    
                    
                    for t_node in classroom_node.getElementsByTagName("teacher"):
                        teacher_id = t_node.getAttribute("id")
                        teacher_obj = teacher.teacher(teacher_id, t_node.getAttribute("last_name"),
                                          t_node.getAttribute("first_name") )
                        classroom_obj.add_teacher(teacher_obj)
                    
                    for s_node in classroom_node.getElementsByTagName("student"):
                        student_id = s_node.getAttribute("id")
                        student_obj = student.student(student_id, s_node.getAttribute("last_name"),
                                          s_node.getAttribute("first_name"), student_id[0] )
                        classroom_obj.add_student(student_obj)   
   
"""
output all objects from schools to a xml file
"""            
def write2xml(schools, outfile):

    doc = xml.dom.minidom.Document()
    
    # Create the root element
    school = doc.createElementNS("http://www.wirelessgeneration.com/wgen.xsd" , "school")
    school.setAttribute("id", "100")
    school.setAttribute("name", "WGen School")
    school.setAttribute("xmlns", "http://www.wirelessgeneration.com/wgen.xsd")
    doc.appendChild(school)
    
    for g in schools.get("100").grades.keys():
        grade = doc.createElement("grade")
        grade.setAttribute("id", g)
        school.appendChild(grade)
        
        for c in sorted (schools.get("100").grades[g].classrooms.keys()):
            classroom_obj = schools.get("100").grades[g].classrooms[c]
            classroom = doc.createElement("classroom")
            classroom.setAttribute("id", c)
            classroom.setAttribute("name", classroom_obj.name)
            grade.appendChild(classroom)
        
            
            for t in sorted (classroom_obj.teachers.keys()):
                teacher_obj = classroom_obj.teachers[t]
                teacher = doc.createElement("teacher")
                teacher.setAttribute("id", teacher_obj.id)
                teacher.setAttribute("first_name", teacher_obj.first_name)
                teacher.setAttribute("last_name", teacher_obj.last_name)
                classroom.appendChild(teacher)
            
            for t in sorted (classroom_obj.students.keys()):
                student_obj = classroom_obj.students[t]
                student = doc.createElement("student")
                student.setAttribute("id", student_obj.id)
                student.setAttribute("first_name", student_obj.first_name)
                student.setAttribute("last_name", student_obj.last_name)
                classroom.appendChild(student)
                
    file = open(outfile,"w")
    file.write(doc.toprettyxml())
    file.close()
    
    